package com.weiyuwqj.server.service.asset.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.weiyuwqj.api.constant.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasItem;
import com.weiyuwqj.api.model.SysAssetDeposit;
import com.weiyuwqj.api.model.SysAssetPicture;
import com.weiyuwqj.api.model.SysAssetProduct;
import com.weiyuwqj.api.model.vo.SysAssetProductVo;
import com.weiyuwqj.api.service.BasItemService;
import com.weiyuwqj.api.service.SysAssetDepositService;
import com.weiyuwqj.api.service.SysAssetPictureService;
import com.weiyuwqj.api.service.SysAssetProductService;
import com.weiyuwqj.server.mapper.SysAssetProductMapper;
import com.weiyuwqj.server.service.base.BaseServiceImpl;

/**
 * @author cx
 * @className:SysAssetCompanyServiceImpl
 * @Description: 资产方产品serviceimpl
 * @date:2017年3月25日上午10:42:27
 */
@Service("sysAssetProductService")
public class SysAssetProductServiceImpl extends BaseServiceImpl<SysAssetProduct> implements SysAssetProductService {

    @Autowired
    private SysAssetProductMapper sysAssetProductMapper;
    @Autowired
    private SysAssetDepositService sysAssetDepositService;
    @Autowired
    private SysAssetPictureService sysAssetPictureService;
    @Autowired
    private BasItemService basItemService;


    @Override
    public PageList getPageList(final Paginator p, final Map<String, Object> param) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        final List<SysAssetProductVo> list = this.sysAssetProductMapper.getPageList(param);
        SysAssetDeposit sysAssetDeposit = null;
        BasItem item = null;
        for (SysAssetProductVo sysAssetProductVo : list) {
            sysAssetDeposit = this.sysAssetDepositService.findByCompanyId(sysAssetProductVo.getCompanyId());
            if (sysAssetDeposit != null) {
                sysAssetProductVo.setDepositAmount(sysAssetDeposit.getTotalAmount() - sysAssetDeposit.getUsedAmount());
            }
            if (sysAssetProductVo.getItemId() > 0) {
                item = this.basItemService.selectByKey(sysAssetProductVo.getItemId());
                if (item != null) {
                    sysAssetProductVo.setItemName(item.getItemName());
                }
            }
        }
        return new PageList(list);
    }


    @Override
    public void saveAssetProduct(final SysAssetProductVo sysAssetProduct) {
        final SysAssetDeposit sysAssetDeposit = this.sysAssetDepositService.findByCompanyId(sysAssetProduct.getCompanyId());
        if (sysAssetDeposit == null) {
            throw new CrowdException("sys.asset.add.deposit.before");
        }
        if (sysAssetProduct.getId() != null && sysAssetProduct.getId() > 0) {
            this.updateNotNull(sysAssetProduct);
        } else {
            this.save(sysAssetProduct);
        }

    }

    @Override
    public Integer findByItemId(final Integer itemId) {
        final SysAssetProductVo productVo = this.sysAssetProductMapper.findByItemId(itemId);
        if (productVo != null) {
            return productVo.getCompanyId();
        }
        return null;
    }

	@Override
	public SysAssetProductVo findById(final Integer id) {
		SysAssetProductVo assetProductVo = this.sysAssetProductMapper.findById(id);
		//插入系统配置的债权手续费率
		assetProductVo.setDebtServeFeeRate(new BigDecimal(Global.getDebtServeFeeRate()));
		return assetProductVo;
	}


    @Override
    public void updateProductItemId(final Integer itemId) {
        this.sysAssetProductMapper.updateByItemId(itemId);
    }

    @Override
    public void pushAssetPackage(final Integer productId, final Integer debtTransferable, final Date endTime, Map map, BigDecimal debtServeFeeRate, BigDecimal itemRate) {
        SysAssetProductVo sysAssetProduct = this.findById(productId);
        if (sysAssetProduct == null) {
            throw new CrowdException("sys.asset.product.not_exists");
        }
        if (sysAssetProduct.getItemId() > 0) {
            throw new CrowdException("sys.asset.product.item.already.push");
        }
        //获得产品图片
        final List<SysAssetPicture> picList = this.sysAssetPictureService.getList(productId);
        if (picList == null || picList.isEmpty()) {
            throw new CrowdException("sys.asset.picture.id.not_exists");
        }
        final List<String> list = new ArrayList<String>();
        for (SysAssetPicture sysAssetPicture : picList) {
            list.add(sysAssetPicture.getUpdateUrl() + "," + sysAssetPicture.getType());
        }
        sysAssetProduct.setPicList(list);
        int result = this.sysAssetDepositService.updateByAmount(sysAssetProduct.getCompanyId(), sysAssetProduct.getAmount());
        if (result <= 0) {
            throw new CrowdException("sys.asset.deposit.amount.not.enough");
        }
        sysAssetProduct.setDebtTransferable(debtTransferable);
        sysAssetProduct.setEndTime(endTime);
        sysAssetProduct.setItemIsnew(Integer.valueOf(map.get("itemIsnew").toString()));
        sysAssetProduct.setItemIsrecommend(Integer.valueOf(map.get("itemIsrecommend").toString()));
//        sysAssetProduct.setFundSecurity(map.get("fundSecurity").toString());
//        sysAssetProduct.setRiskControlMeasures(map.get("riskControlMeasures").toString());
        BigDecimal itemSingleMinInvestment = BigDecimal.ZERO;
        if(map.get("itemSingleMinInvestment")!=null){
            itemSingleMinInvestment = new BigDecimal(map.get("itemSingleMinInvestment").toString());
        }
        Integer itemId = this.basItemService.assetsShiftBid(sysAssetProduct, debtServeFeeRate,itemRate,itemSingleMinInvestment);
        final HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("itemId", itemId);
        params.put("id", sysAssetProduct.getId());
        this.sysAssetProductMapper.updateItemIdById(params);
    }
}
